package lol.oxguy3.droidmenuz;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ProgressBar;
import android.widget.Toast;

public class MenuzActivity extends Activity implements DialogInterface.OnClickListener {
	
	Context cxt;
	ProgressBar progbar;
	public Handler handy = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			progbar.incrementProgressBy(5);
		}
	};
	boolean isRunning = false;
	Thread bgthread;
	Object derp = new Object();
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        cxt = this;
        progbar = (ProgressBar)findViewById(R.id.theProgressBar);
        
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu m) {
    	MenuInflater pump = getMenuInflater();
    	pump.inflate(R.menu.something, m);

		return true;
    }
    
    public void resumeThread() {
    	isRunning = true;
    	synchronized (derp) {
        	derp.notify();
    	}
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem mi) {
    	switch (mi.getItemId()) {
    	case R.id.startItem:
    		if (!isRunning) {
	    		resumeThread();
    		} else {
    			Toast.makeText(cxt, "Thread has already been started.", Toast.LENGTH_SHORT).show();
    		}
    		return true;
    		
    	case R.id.stopItem:
    		if (isRunning) {
    			isRunning = false;
    		} else {
    			Toast.makeText(cxt, "Thread has already been stopped.", Toast.LENGTH_SHORT).show();
    		}
    		return true;
    		
    	case R.id.pauseItem:
    		AlertDialog.Builder adb = new AlertDialog.Builder(this);
    		adb.setTitle("Warning: may cause errors!");
    		adb.setMessage("Oracle says that pausing threads is bad, and Oracle likes to sue people. You sure you want to defy Oracle?");
    		adb.setPositiveButton("Yes", (android.content.DialogInterface.OnClickListener) this);
    		adb.setNegativeButton("No", (android.content.DialogInterface.OnClickListener) this);
    		adb.show();
    		return true;
    		
    	default:
    		Toast.makeText(cxt, "dude wtf did you press", Toast.LENGTH_SHORT).show();
    		return super.onOptionsItemSelected(mi);
    	}
    }
    
    @Override
    public void onStart() {
    	super.onStart();
    	progbar.setProgress(0);
    	
    	bgthread = new Thread(new Runnable() {
    		public void run() {
    			try {
    				for (int i = 0; /*i < 20 &&*/ isRunning; i++) {
    					Thread.sleep(300);
    					if (progbar.getProgress() >= progbar.getMax()) {
    						progbar.setProgress(0);
    					} else {
    						handy.sendMessage(handy.obtainMessage());
    					}
    					synchronized (derp) {
	    					if (!isRunning) {
	    						derp.wait();
	    					}
    					}
    				}
    			} catch (Throwable t) { }
    		}
    	});
    	isRunning = true;
    	bgthread.start();
    }
    
    @Override
	public void onStop() {
		super.onStop();
		isRunning = false;
	}

	public void onClick(DialogInterface di, int btnid) {
		if (btnid==DialogInterface.BUTTON_POSITIVE) {
			if (bgthread.isAlive()) {
	    		if (bgthread.isInterrupted()) {
	    			bgthread.resume();
	    		} else {
	    			bgthread.suspend();
	    		}
			} else {
				Toast.makeText(cxt, "Thread must be running to be paused/unpaused", Toast.LENGTH_SHORT);
			}
			
		} else if (btnid==DialogInterface.BUTTON_NEGATIVE) {
			//Toast.makeText(this, "aww", Toast.LENGTH_SHORT).show();
			/** do nothing */
		}
	}
}